import {defineStore} from 'pinia'
import {message} from 'ant-design-vue';
import zjRequest from "@/services";
import {IResponseData} from "@/services/types";
import {IActions, IPrettyDetailParam, IState, PRETTY_URL_TYPE} from "@/store/prettyModule/types";

const usePrettyStore = defineStore<any, IState, any, IActions>('prettyModule', {
  state: () => {
    return {
      prettyInfo: {
        list: [],
        page_info: {
          current_page: 1,
          page_size: 10,
          total_count: 0
        }
      },
      currentPrettyDetail: {
        id: 0,
        mobile: "",
        price: 0,
        level: 1,
        status: 1,
      }
    }
  },
  actions: {
    // 获取靓号详情
    async requestPrettyDetailAction(params: IPrettyDetailParam) {
      const res: IResponseData = await zjRequest.get({
        url: PRETTY_URL_TYPE.PRETTY_DETAIL,
        params
      })
      if (res.code === 0) {
        this.currentPrettyDetail = res.data
      }
    },
    // 获取靓号列表
    async requestPrettyListAction({current_page, page_size}) {
      const res: IResponseData = await zjRequest.get({
        url: PRETTY_URL_TYPE.PRETTY_LIST,
        params: {
          current_page,
          page_size
        }
      })
      if (res.code === 0) {
        this.prettyInfo = res.data
      }
    },
    // 添加靓号
    async addPrettyAction(params) {
      const res: IResponseData = await zjRequest.post({
        url: PRETTY_URL_TYPE.PRETTY_ADD,
        data: params
      })
      if (res.code === 0) {
        message.success("添加成功")
        this.requestPrettyListAction({
          current_page: this.prettyInfo.page_info.current_page,
          page_size: this.prettyInfo.page_info.page_size
        })
        return true
      } else {
        return false
      }
    },
    // 删除靓号
    async deletePrettyAction(params) {
      const res: IResponseData = await zjRequest.post({
        url: PRETTY_URL_TYPE.PRETTY_DELETE,
        data: params
      })
      if (res.code === 0) {
        message.success("删除成功")
        this.requestPrettyListAction({
          current_page: this.prettyInfo.page_info.current_page,
          page_size: this.prettyInfo.page_info.page_size
        })
        return true
      } else {
        return false
      }
    },
    async updatePrettyAction(params) {
      const res: IResponseData = await zjRequest.post({
        url: PRETTY_URL_TYPE.PRETTY_UPDATE,
        data: params
      })
      if (res.code === 0) {
        message.success("修改成功")
        this.requestPrettyListAction({
          current_page: this.prettyInfo.page_info.current_page,
          page_size: this.prettyInfo.page_info.page_size
        })
        return true
      } else {
        return false
      }
    }
  }
})
export default usePrettyStore
